import {
  Body,
  Controller,
  Get,
  HttpException,
  Post,
  Query,
  UseGuards,
} from '@nestjs/common';
import { AuthorBaseControll } from './authorBase.controller';
import { ArticleService } from '../services/article.service';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { AuthGuard } from '../guards/authority.guard';
import { AddArticleDto, GetArticleByIdDto } from '../dtos/article.dto';

@Controller('/article')
@ApiTags('文章中心')
@UseGuards(AuthGuard)
export class ArticleController extends AuthorBaseControll {
  constructor(private readonly articleService: ArticleService) {
    super();
  }

  @ApiOperation({ summary: '获取文章类型' })
  @Get('/getArticleType')
  async getArticleTypeControll() {
    return this.articleService.getArticleTypeServe();
  }

  @ApiOperation({ summary: '添加文章' })
  @Post('/addArticle')
  async addArticleControll(@Body() articleContent: AddArticleDto) {
    const result = await this.articleService.addArticleServe(articleContent);
    if (result.affectedRows === 1) {
      return {
        message: '新增文章成功',
      };
    } else {
      throw new HttpException('新增失败', 401);
    }
  }

  @ApiOperation({ summary: '获取文章列表' })
  @Get('/getArticleList')
  async getArticleListController() {
    return this.articleService.getArticleListServe();
  }

  @ApiOperation({ summary: '获取文章详情' })
  @Get('/getArticleConById')
  async getArticleConByIdControll(@Query() articleId: GetArticleByIdDto) {
    console.log(articleId);
    return await this.articleService.getArticleConByIdServe(articleId);
  }
}
